<?php

namespace app\index\controller;

use think\Request;
use think\Db;

class Group extends BaseController
{
    // 显示资源列表
    public function index()
    {
        // 班期信息
        $row = Db::name('class')
            ->field(true)
            ->order('class_name', 'DESC')
            ->select();
        return view('group/index', ['list' => $row]);
    }

    // 修改显示状态
    public function coverChange()
    {
        $id = input('put.id');
        $cover = (input('put.cover') == 'true') ? 1 : 0;
        $row = Db::name('class')->where('id', $id)->update(['cover' => $cover]);
        if ($row) {
            if ($cover === 1) {
                $res = ['code' => 200, 'status' => 'success', 'msg' => '已设置为在主页显示'];
            } else {
                $res = ['code' => 200, 'status' => 'primary', 'msg' => '已取消 在主页的显示'];
            }
        } else {
            $res = ['code' => 4004, 'status' => 'danger', 'msg' => '设置失败，请重试！'];
        }
        return $res;
    }

    // 修改注册状态
    public function statusChange()
    {
        $id = input('put.id');
        $status = (input('put.status') == 'true') ? 1 : 0;
        $row = Db::name('class')->where('id', $id)->update(['status' => $status]);
        if ($row) {
            if ($status === 1) {
                $res = ['code' => 200, 'status' => 'success', 'msg' => '已设置为授课状态，注册页面会出现该班期'];
            } else {
                $res = ['code' => 200, 'status' => 'primary', 'msg' => '已取消授课状态'];
            }
        } else {
            $res = ['code' => 4004, 'status' => 'danger', 'msg' => '设置失败，请重试！'];
        }
        return $res;
    }


    // 保存新建的班期
    public function save(Request $request)
    {
        $post = $request->post();
        foreach ($post as $v) {
            if ($v == '') {
                $res = ['code' => 4001, 'status' => 'danger', 'msg' => '录入信息不完整!'];
                return $res;
            }
        }
        $class_name = $post['class_name'];
        // 班期不得含有空白字符
        if (preg_match('/\s/', $class_name)) {
            $res = ['code' => 4002, 'status' => 'primary', 'msg' => '班期名称中不得含有空白字符！'];
        } else {
            $class = Db::name('class')->where('class_name', $class_name)->find();
            if ($class === null) {
                $row = Db::name('class')->data($post)->insert();
                if ($row === 1) {
                    $res = ['code' => 200, 'status' => 'success', 'msg' => '班期信息，录入成功！'];
                } else {
                    $res = ['code' => 4004, 'status' => 'danger', 'msg' => '班期录入失败，请重试！'];
                }
            } else {
                $res = ['code' => 4004, 'status' => 'danger', 'msg' => '班期名称已存在！'];
            }
        }
        return $res;
    }


    // 删除指定班期
    public function delete($id)
    {
        $row = Db::name('class')->delete($id);
        if ($row) {
            $res = ['code' => 200, 'status' => 'success', 'msg' => '删除班期记录成功!'];
        } else {
            $res = ['code' => 4005, 'status' => 'warning', 'msg' => '删除班期记录失败,请重试!'];
        }
        return $res;
    }
}
